package com.sandwich.admin.service.impl;


import com.sandwich.admin.domain.CategoryEntity;
import com.sandwich.admin.domain.repository.CategoryRepository;
import com.sandwich.admin.service.CategoryService;
import com.sandwich.api.apibean.v1.CategoryRequest;
import com.sandwich.common.Commons;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description 分类信息
 * @date 2021-07-28
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;

    /**
     * 查询
     *
     * @param pk
     */
    public CategoryEntity find(long pk) {
        return categoryRepository.findById(pk).get();
    }

    @Override
    public List<CategoryEntity> findAll() {
        return categoryRepository.findAllByStatus(Commons.ENABLE_STATUS);
    }

    /**
     * 新增或编辑
     *
     * @param categoryRequest
     */
    public CategoryEntity save(CategoryRequest categoryRequest) {
        CategoryEntity categoryEntity = new CategoryEntity();
        categoryEntity.setPk(categoryRequest.getPk());
        categoryEntity.setName(categoryRequest.getName());
        categoryEntity.setGlobalPlatformPk(categoryRequest.getGlobalPlatformPk());
        categoryEntity.setStatus(categoryRequest.getStatus());
        categoryEntity.setCreatedAt(categoryRequest.getCreatedAt());
        categoryEntity.setUpdatedAt(categoryRequest.getUpdatedAt());
        categoryRepository.save(categoryEntity);
        return categoryEntity;
    }

}